﻿using AutoMapper;
using MediatR;
using S.PregnancyTest.Domain;
using S.PregnancyTest.Domain.Model;
using S.PregnancyTest.Read.API.Application.Command;
using S.PregnancyTest.Read.API.Dto;

namespace S.PregnancyTest.Read.API.Application.CommandHandlers
{
    public class GetPregnancyTestByEarTagHandler : IRequestHandler<GetPregnancyTestByEarTagCommand, PregnancyTestListDto>
    {

        private readonly MyDbContext dbContext;

        private readonly IMapper mapper;

        public GetPregnancyTestByEarTagHandler(MyDbContext dbContext, IMapper mapper)
        {
            this.dbContext = dbContext;
            this.mapper = mapper;
        }

        public async Task<PregnancyTestListDto> Handle(GetPregnancyTestByEarTagCommand request, CancellationToken cancellationToken)
        {
            PregnancyTestModel pregnancyTestModel = dbContext.PregnancyTestModel.Where(x => x.EarTag == request.EarTag).FirstOrDefault();

            return mapper.Map<PregnancyTestListDto>(pregnancyTestModel);
        }
    }
}
